package com.brainhealth.web.controller.customer;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.core.domain.TreeSelect;
import com.brainhealth.common.enums.LearnCatetoryType;
import com.brainhealth.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.customer.domain.LearnCategory;
import com.brainhealth.customer.service.ILearnCategoryService;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.common.core.page.TableDataInfo;

/**
 * 学习库分类树Controller
 * 
 * @author wolong
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/customer/learncategory")
public class LearnCategoryController extends BaseController
{
    @Autowired
    private ILearnCategoryService learnCategoryService;

    /**
     * 查询学习库分类树列表
     */
    @PreAuthorize("@ss.hasPermi('customer:learncategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(LearnCategory learnCategory)
    {
        startPage();
        List<LearnCategory> list = learnCategoryService.selectLearnCategoryList(learnCategory);
        return getDataTable(list);
    }

    /**
     * 导出学习库分类树列表
     */
    @PreAuthorize("@ss.hasPermi('customer:learncategory:export')")
    @Log(title = "学习库分类树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LearnCategory learnCategory)
    {
        List<LearnCategory> list = learnCategoryService.selectLearnCategoryList(learnCategory);
        ExcelUtil<LearnCategory> util = new ExcelUtil<LearnCategory>(LearnCategory.class);
        util.exportExcel(response, list, "学习库分类树数据");
    }

    /**
     * 获取学习库分类树详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:learncategory:query')")
    @GetMapping(value = "/{learnCategoryId}")
    public AjaxResult getInfo(@PathVariable("learnCategoryId") Long learnCategoryId)
    {
        return success(learnCategoryService.selectLearnCategoryByLearnCategoryId(learnCategoryId));
    }

    /**
     * 新增学习库分类树
     */
    @PreAuthorize("@ss.hasPermi('customer:learncategory:add')")
    @Log(title = "学习库分类树", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LearnCategory learnCategory)
    {
        return toAjax(learnCategoryService.insertLearnCategory(learnCategory));
    }

    /**
     * 修改学习库分类树
     */
    @PreAuthorize("@ss.hasPermi('customer:learncategory:edit')")
    @Log(title = "学习库分类树", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LearnCategory learnCategory)
    {
        return toAjax(learnCategoryService.updateLearnCategory(learnCategory));
    }

    /**
     * 删除学习库分类树
     */
    @PreAuthorize("@ss.hasPermi('customer:learncategory:remove')")
    @Log(title = "学习库分类树", businessType = BusinessType.DELETE)
    @DeleteMapping("/{learnCategoryIds}")
    public AjaxResult remove(@PathVariable Long[] learnCategoryIds)
    {
        return toAjax(learnCategoryService.deleteLearnCategoryByLearnCategoryIds(learnCategoryIds));
    }

    /**
     * 查询学习库列表
     */
    @PreAuthorize("@ss.hasPermi('customer:learn:list')")
    @GetMapping("/categoryTree")
    public AjaxResult categoryTree()
    {
        List<TreeSelect> list = learnCategoryService.getLearnCategoryTree();
        return success(list);
    }

    /**
     * 学习大厅中所有的分类
     *
     * @return
     */
    @Anonymous
    @PostMapping("/getLearnCategoryList")
    public AjaxResult getLearnCategory(@RequestBody Map<String, Object> map)
    {
        String categoryType = map.get("categoryType").toString();
        if (StringUtils.isNullOrEmpty(categoryType))
        {
            return AjaxResult.error("学习库分类类型不能为空！");
        }
        LearnCatetoryType learnCatetoryType = LearnCatetoryType.valueOf(categoryType);
        List<LearnCategory> learnCategoryList = learnCategoryService.getLearnCategoryList(learnCatetoryType);
        return AjaxResult.success(learnCategoryList);
    }
}
